<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" class="filter-container">
      <el-form-item>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toUserGroupNew"
        >添加用户群组</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="群组名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.settime | moment('YYYY-MM-DD') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toUserGroupEdit(scope.row.id)"
          >群组下相关菜单</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="removeUserGroup(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getUserGroupList,
  removeUserGroup
} from '@/api/userGroup'

export default {
  name: 'UserGroupList',
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getUserGroupList().then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    removeUserGroup(id) {
      this.$confirm('此操作将永久删除该群组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeUserGroup({ id }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    toUserGroupNew() {
      this.$router.push('/setting/user-group/new')
    },
    toUserGroupEdit(id) {
      this.$router.push(`/setting/user-group/edit/${id}`)
    }
  }
}
</script>
